package com.itheima.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.AccountMapper;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper mapper;

    @Override
    public boolean deduct(String userAccount, Integer cost) {
        //接收参数查询用户在不在:条件查询,根据名字查询
        Account account = mapper.ByName(userAccount);
        if (account == null) {
            throw new RuntimeException("该用户不存在!!!");
        }
        //判断金额是否为正,并且减去的金额要大于数据的
        if (account.getAmount() - cost <= 0) {
            throw new RuntimeException("余额不足....");
        }

        int i = mapper.setMoneyByUserName(userAccount, cost);
        //判断有没有修改成功,成功i>0 返回true
      /*  if (i>0){
            return true;
        }
        return false;*/
        return i>0;
    }
}
